import java.util.Collections;
import java.util.Properties;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

public class MsgConsumer {
	public static void main(String[] args) {
		Properties properties = new Properties();
        //从这个地址接收消息，和生产者的一致   
        properties.put("bootstrap.servers", "localhost:9092");
        //需要指定消费组
        properties.put("group.id", "myKafka");
        properties.put("key.deserializer",
                "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put("value.deserializer",
                "org.apache.kafka.common.serialization.StringDeserializer");

        KafkaConsumer<String, String> myConsumer = new KafkaConsumer<String, String>(properties);

        myConsumer.subscribe(Collections.singletonList("myQueue"));
        
        try {
            ConsumerRecords<String, String> consumeRecords = myConsumer.poll(1000);
            for (ConsumerRecord<String, String> oneRecord : consumeRecords){
                // print the key and value for the consumer records.
                System.out.printf("key is: " + oneRecord.key());
                System.out.printf("value is: " + oneRecord.value());            	
            }
        }
        catch(Exception e){
        	e.printStackTrace();
        }
        finally{
        	if(myConsumer != null){
        		myConsumer.close();
        	}
        	properties.clear();
        }
        
        
	}
}
